import React from 'react';
import { HashRouter as Router, Route, Redirect, Switch } from 'react-router-dom'
import { withRouter } from 'react-router-dom';
import { toJS } from 'mobx';
import { Provider, inject, observer } from 'mobx-react';
import { asyncComponent } from 'react-async-component';
import { DocumentTitle } from "weaver-mobile-page";
import { Tools } from 'weaver-mobile';
import weaWorkrelateFileUtils from '../../util/wea-workrelate-file'

const getLabel = Tools.getLabel

weaWorkrelateFileUtils.addWindowFunc()

const coms = {
  TaskHome: asyncComponent({
    resolve: () => import('./taskHome/taskHome')
  }),
  TaskMain: asyncComponent({
    resolve: () => import('./taskMain/taskMain')
  }),
  TaskMainAdvancedSearch: asyncComponent({
    resolve: () => import('./taskMainAdvancedSearch/taskMainAdvancedSearch')
  }),
  NewTask: asyncComponent({
    resolve: () => import('./newTask/newTask')
  }),
  TaskDetail: asyncComponent({
    resolve: () => import('./taskDetail/taskDetail')
  })
}

@inject('taskMainStore')
@withRouter
@observer
class Index extends React.Component {

  constructor(props) {
    super(props)
    this.state = {}
  }

  componentWillMount() {

  }

  getCondtypeTitle = (condtype) => {
    const { bottomMenuDatas } = this.props.taskMainStore
    let title = ""
    bottomMenuDatas.forEach((d, i) => {
      if (d.key == condtype) {
        title = d.menuname
      }
    })
    return title
  }

  render() {
    let { condtype, defaultCondtype } = this.props.taskMainStore
    if (!condtype) {
      condtype = defaultCondtype
    }
    const data = [
      { match: /^\/taskHome\/taskMain/i, title: () => this.getCondtypeTitle(condtype) },
      { match: /^\/taskHome\/taskMain\/advancedSearch/i, title: () => getLabel(347,'高级搜索') },
      { match: /^\/taskHome\/taskMain\/subUser/i, title: () => getLabel(17494,"所有下属") },
      { match: /^\/taskHome\/taskMain\/tagManage/i, title: () => getLabel(30884,'标签设置') },
      // { match: /^\/taskHome\/taskMain\/tagManage\/tagDetail/i, title: () => getLabel('516383', '编辑标签') },
      { match: /^\/taskHome\/newTask/i, title: () => getLabel(15266, '新建任务') },
      { match: /^\/taskHome\/taskDetail/i, title: () => getLabel(508789,'任务详情') }
    ]

    return ( 
      <div className="workrelate-common-container">
        <DocumentTitle data={data} defaultTitle="" />
        <Switch>
          <Route name='taskHome' breadcrumbName={getLabel(1332,'任务')} path='/taskHome' render={() => {
            return (
              <coms.TaskHome>
                <Switch>
                  <Route name='taskMain' breadcrumbName={getLabel(18505,'任务列表')} path='/taskHome/taskMain/:condtype' component={coms.TaskMain} />
                  <Route name='newTask' breadcrumbName={getLabel(15266, '新建任务')} path='/taskHome/newTask' component={coms.NewTask} />
                  <Route name='taskDetail' breadcrumbName={getLabel(508789,'任务详情')} path='/taskHome/taskDetail/:id' component={coms.TaskDetail} />
                  <Redirect key={new Date().getTime() + ""} path="/taskHome" to={{ pathname: "/taskHome/taskMain/" + condtype }} />
                </Switch>
              </coms.TaskHome>
            )
          }} />
        </Switch>
      </div>
    )
  }
}

export default Index